package com.hogocloud.myframe.weight;

import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.customview.widget.ViewDragHelper;

import com.hogocloud.corelibrary.utils.LogUtils;

public class VDHLayout extends ConstraintLayout {
    private ViewDragHelper mDrager;

    private Point mAutoBackOriginPosA = new Point();
    private Point mAutoBackOriginPosB = new Point();

    private Point mViewCurrentPos = new Point();

    private Rect mDragZoneRectA = new Rect();
    private Rect mDragZoneRectB = new Rect();
    private View mAutoBackViewA;
    private View mAutoBackViewB;
    private View mDragZoneA;
    private View mDragZoneB;

    public VDHLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mDrager = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                //mEdgeTrackerView禁止直接移动
                return child == mAutoBackViewA || child == mAutoBackViewB;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                return left;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return top;
            }

            @Override
            public void onViewPositionChanged(@NonNull View changedView, int left, int top, int dx, int dy) {
                super.onViewPositionChanged(changedView, left, top, dx, dy);
                mViewCurrentPos.x = left + (changedView.getRight() - changedView.getLeft()) / 2;
                mViewCurrentPos.y = top + (changedView.getBottom() - changedView.getTop()) / 2;
                LogUtils.INSTANCE.e("position  " + left + "," + top + "," + dx + "," + dy);
            }

            //手指释放的时候回调
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                //mAutoBackView手指释放时可以自动回去
                if (releasedChild == mAutoBackViewA) {
                    if (mDragZoneRectA.contains(mViewCurrentPos.x, mViewCurrentPos.y)) {
                        mDrager.settleCapturedViewAt((mDragZoneRectA.right - mDragZoneRectA.left) / 2,
                                (mDragZoneRectA.bottom - mDragZoneRectA.top) / 2);
                    } else {
                        mDrager.settleCapturedViewAt(mAutoBackOriginPosA.x, mAutoBackOriginPosA.y);
                    }
                } else if (releasedChild == mAutoBackViewB) {
                    mDrager.settleCapturedViewAt(mAutoBackOriginPosB.x, mAutoBackOriginPosB.y);
                }
                invalidate();
            }

            //在边界拖动时回调
            @Override
            public void onEdgeDragStarted(int edgeFlags, int pointerId) {

            }
        });
        mDrager.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return mDrager.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDrager.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        if (mDrager.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        mDragZoneRectA.left = mDragZoneA.getLeft();
        mDragZoneRectA.top = mDragZoneA.getTop();
        mDragZoneRectA.right = mDragZoneA.getRight();
        mDragZoneRectA.bottom = mDragZoneA.getBottom();

        mDragZoneRectB.left = mDragZoneB.getLeft();
        mDragZoneRectB.top = mDragZoneB.getTop();
        mDragZoneRectB.right = mDragZoneB.getRight();
        mDragZoneRectB.bottom = mDragZoneB.getBottom();

        mAutoBackOriginPosA.x = mAutoBackViewA.getLeft();
        mAutoBackOriginPosA.y = mAutoBackViewA.getTop();

        mAutoBackOriginPosB.x = mAutoBackViewB.getLeft();
        mAutoBackOriginPosB.y = mAutoBackViewB.getTop();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mDragZoneA = getChildAt(0);
        mDragZoneB = getChildAt(1);

        mAutoBackViewA = getChildAt(2);
        mAutoBackViewB = getChildAt(3);

    }
}